/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.time;

import java.text.DateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Locale;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class PlaylistRecordRestriction {

	private Calendar	fromTime;
	private Calendar	toTime;
	private String[]	days;

	public PlaylistRecordRestriction(Calendar from, Calendar to, String[] days) {
		// assert (from.compareTo(to) < 0) : from.getTime() + " " +
		// to.getTime();
		this.fromTime = from;
		this.toTime = to;
		this.days = days;
		this.fromTime.set(Calendar.MILLISECOND, 0);
		this.toTime.set(Calendar.MILLISECOND, 0);
		Arrays.sort(days);

	}

	public Calendar getToTime() {
		return this.toTime;
	}

	public Calendar getFromTime() {
		return this.fromTime;
	}

	/**
	 * @precondition this.fromTime!=null && this.toTime!=null
	 * @postcondition
	 * @return
	 */
	public boolean isPlayableNow() {

		if (this.toTime.before(this.fromTime)) {
			System.out.println(this.fromTime.getTime().toString() + " " + this.toTime.getTime().toString());
			return false;
		}
		Calendar now = Calendar.getInstance();
		Duration fullDuration = new Duration(this.fromTime, this.toTime);

		boolean extendsToNextDay = fullDuration.extendsToNextDay(this.fromTime, this.toTime);
		if (extendsToNextDay) {
			// this.toTime.add(Calendar.DAY_OF_MONTH, 1);
			if (now.before(this.toTime)
					&& now.after(this.fromTime)
					&& fullDuration.withInDuration(now)
					&& 0 <= Arrays.binarySearch(this.days,
							now.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH))) {
				return true;
			}
			// this.toTime.add(Calendar.DAY_OF_MONTH, -1);
			return false;
		} else {
			if (now.before(this.toTime)
					&& now.after(this.fromTime)
					&& fullDuration.withInDuration(now)
					&& 0 <= Arrays.binarySearch(this.days,
							now.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH))) {
				return true;
			}
			return false;
		}
	}

	@Override
	public String toString() {
		// from Time
		// int fromYear = this.fromTime.get(Calendar.YEAR);
		// int fromMonth = this.fromTime.get(Calendar.MONTH);
		// int fromDay = this.fromTime.get(Calendar.DAY_OF_MONTH);
		// int fromHour = this.fromTime.get(Calendar.HOUR_OF_DAY);
		// int fromMinute = this.fromTime.get(Calendar.MINUTE);
		// int fromSecond = this.fromTime.get(Calendar.SECOND);
		// // to Time
		// int toYear = this.toTime.get(Calendar.YEAR);
		// int toMonth = this.toTime.get(Calendar.MONTH);
		// int toDay = this.toTime.get(Calendar.DAY_OF_MONTH);
		// int toHour = this.toTime.get(Calendar.HOUR_OF_DAY);
		// int toMinute = this.toTime.get(Calendar.MINUTE);
		// int toSecond = this.toTime.get(Calendar.SECOND);
		DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
		return String.format("%s - %s", df.format(this.fromTime.getTime()), df.format(this.toTime.getTime()));
		// return
		// String.format("Date: %1$d/%2$d/%3$d - %4$d/%5$d/%6$d Time: %7$d:%8$d:%9$d - %10$d:%11$d:%12$d",
		// fromYear, fromMonth, fromDay, toYear, toMonth, toDay, fromHour,
		// fromMinute, fromSecond, toHour, toMinute, toSecond);
	}

	public static void main(String[] args) {
		Calendar f = Calendar.getInstance();
		Calendar t = Calendar.getInstance();
		f.set(2011, 01, 22, 10, 0, 0);
		t.set(2011, 01, 23, 13, 0, 0);
		// System.out.println(f.getTimeInMillis() + " " + t.getTimeInMillis());
		// System.out.println(f.getTime() + " " + t.getTime());
		PlaylistRecordRestriction r = new PlaylistRecordRestriction(f, t, new String[] { "Wednesday", "Thursday" });
		System.out.println(r.isPlayableNow());
		System.out.println(r);
	}

	public boolean isExpired() {
		return (this.toTime.compareTo(Calendar.getInstance())<0);
	}
}